﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.WinUI.BaseForm;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Sec;
using Suzsoft.Smart.Data;

namespace ESOL.WinUI
{
    public partial class frmLogin : frmBaseForm
    {
        public bool IsStartForm = true;
        public frmLogin()
        {
            InitializeComponent();
        }

        private SecUserEntity GetUserAccount()
        {
            SecUserEntity entity = new SecUserEntity();
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("账号不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Focus();
                return null;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("密码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPassword.Focus();
                return null;
            }
            entity = SecUserBs.GetUserByCode(txtUserName.Text);
            if (entity == null)
            {
                MessageBox.Show("账号无效！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserName.Text = "";
                txtUserName.Focus();
                return null;
            }
            if (entity.Password != Common.Encryption.Encrypt(txtPassword.Text))
            {
                MessageBox.Show("账号密码不匹配！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPassword.Text = "";
                txtPassword.Focus();
                return null;
            }
            return entity;
        }

        private void LogionIn()
        {
            try
            {
                ConnConfigurationManager connConfigurationManager = new ConnConfigurationManager();
                if (connConfigurationManager.InitConfiguration() == false)
                {
                    MessageBox.Show("配置信息错误！","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                SecUserEntity user = GetUserAccount();
                if (user != null)
                {
                    Common.CommonParameter.CurrentUser = user;
                    Common.CommonParameter.CurrentUser.PersonEntity = MstPersonBs.GetMstPersonByID(user.PersonId);
                    Common.CommonParameter.CurrentUser.UserRoleCategory = SecUserBs.GetUserRoleCategory(user.UserId);
                    if (IsStartForm)
                    {
                        frmMain frm = new frmMain();
                        frm.LoginForm = this;
                        this.Hide();
                        frm.Show();

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            frmLoginSetting frm = new frmLoginSetting();
            frm.ShowDialog();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            LogionIn();
        }

        private void pxbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pxbLoginIn_Click(object sender, EventArgs e)
        {
            LogionIn();
        }

        private void lblSetting_Click(object sender, EventArgs e)
        {
            frmLoginSetting frm = new frmLoginSetting();
            frm.ShowDialog();
        }

        private void lblSetting_Enter(object sender, EventArgs e)
        {
            lblSetting.LineVisible = true;
        }

        private void lblSetting_Leave(object sender, EventArgs e)
        {
            lblSetting.LineVisible = false;
        }

        private void lblMore_Click(object sender, EventArgs e)
        {

        }

        private void lblMore_Enter(object sender, EventArgs e)
        {
            lblMore.LineVisible = true;
        }

        private void lblMore_Leave(object sender, EventArgs e)
        {
            lblMore.LineVisible = false;
        }

        private void lblForget_Click(object sender, EventArgs e)
        {

        }

        private void lblAskfor_Click(object sender, EventArgs e)
        {

        }

        private void lblAskfor_Enter(object sender, EventArgs e)
        {
            lblAskfor.LineVisible = true;
        }

        private void lblAskfor_Leave(object sender, EventArgs e)
        {
            lblAskfor.LineVisible = false;
        }

        private void lblForget_Enter(object sender, EventArgs e)
        {
            lblForget.LineVisible = true;
        }

        private void lblForget_Leave(object sender, EventArgs e)
        {
            lblForget.LineVisible = false;
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter
                &&string.IsNullOrEmpty(txtUserName.Text)==false
                &&string.IsNullOrEmpty(txtPassword.Text)==false)
            {
                LogionIn();
            }
        }
                
    }
}
